#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023-04-18 11:14
# @Author  : wsh
# @File    : send_email.py
import random
import ssl


import requests
import requests
import json

# smtplib 用于邮件的发信动作
import smtplib
# email 用于构建邮件内容
from email.mime.text import MIMEText
# 构建邮件头
from email.header import Header
from email.message import EmailMessage

import time
from threading import Timer


def get_email_code(receiver_email):
    # 这里我调用接口了，如果不调用 可以直接删除
    # xg_url = ''
    # 无需安装第三方库
    # key = 'xtoagvbhvtngebef'  # 换成你的QQ邮箱SMTP的授权码(QQ邮箱设置里)
    key = 'QAKCMKUEXTFAIWTU'  # 换成你的QQ邮箱SMTP的授权码(QQ邮箱设置里)
    EMAIL_ADDRESS = 'tel18081694292@163.com'  # 换成你的邮箱地址
    EMAIL_PASSWORD = key
    # smtp = smtplib.SMTP('smtp.qq.com', 25)
    context = ssl.create_default_context()
    sender = EMAIL_ADDRESS  # 发件邮箱
    receiver = [receiver_email]
    # 收件邮箱

    subject = "储存敏感度分析"
    # 这里我调用了自己的接口，如果不需要直接将body改为 body = '正文'
    code = random_six_password()
    body = "您的验证码为：{}，5分钟内有效".format(code)
    msg = EmailMessage()
    msg['subject'] = subject  # 邮件主题
    msg['From'] = sender
    msg['To'] = receiver
    msg.set_content(body)  # 邮件内容

    with smtplib.SMTP_SSL("smtp.163.com", 465, context=context) as smtp:
        smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
        smtp.send_message(msg)
    return code

def random_six_password():
    passwd = ""
    for i in range(6):
        passwd += str(random.randint(0, 9))
    return passwd
